import { InjectionKey } from 'vue'
import { createStore, Commit, Store, useStore as baseUseStore } from 'vuex'
import {RootStateTypes} from './interface/index'

// 获取文件集合
const modulesFiles = import.meta.globEager('./modules/*.ts');
const pathList: string[] = []

for(const path in modulesFiles) {
  pathList.push(path)
}

const modules = pathList.reduce((modules: { [x: string]: any }, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, '$1');
  const value = modulesFiles[modulePath]
  modules[moduleName] = value.default;
  return modules
}, {})

// 创建store
export const store = createStore<RootStateTypes>({modules})

export const key: InjectionKey<Store<RootStateTypes>> = Symbol()

export const useStore = function(){
  return baseUseStore(key)
}